package com.sudoku.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.sudoku.background.BackgroundManager;
import com.sudoku.observer.Event;
import com.sudoku.observer.Observer;
import com.sudoku.observer.Subject;
import com.sudoku.utility.Constants;
import com.sudoku.utility.Locator;

public class PuzzleLoadingScreen implements Screen, Constants, Observer {

	private BitmapFont font, backgroundFont;
	private SpriteBatch batch;
	private Stage stage;
	private boolean createPuzzle;
	private float timer;
	private int line;
	private BackgroundManager background;

	public PuzzleLoadingScreen(Subject subject) {
		background = new BackgroundManager();
		batch = new SpriteBatch();
		line = 0;
		subject.addObserver(this);
		createPuzzle = false;
		FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal(FONT));
		FreeTypeFontParameter param = new FreeTypeFontParameter();
		param.size = (int) (80);
		font = generator.generateFont(param);
		font.setColor(Color.BLACK);

		param.size = (int) (40);
		backgroundFont = generator.generateFont(param);
		backgroundFont.setColor(new Color(0f, 0f, 0f, 0.3f));
		generator.dispose();

		timer = 2;

		stage = new Stage(Locator.getViewport());

		batch.setTransformMatrix(stage.getCamera().view);
		batch.setProjectionMatrix(stage.getCamera().projection);
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		ShapeRenderer sh = new ShapeRenderer();
		sh.setTransformMatrix(stage.getCamera().view);
		sh.setProjectionMatrix(stage.getCamera().projection);
		sh.begin(ShapeType.Filled);
		sh.setColor(1, 1, 1, 1);
		sh.rect(0, 0, ACTUAL_VIRTUAL_WIDTH, ACTUAL_VIRTUAL_HEIGHT);
		sh.end();

		stage.act(delta);
		stage.draw();

		batch.begin();
		background.render(batch, backgroundFont);
		batch.end();

		background.update(delta);

		if (timer >= 2f) {
			timer = 0;
			line = MathUtils.random(0, liners.length - 1);
		}
		batch.begin();
		font.draw(batch, liners[line], ACTUAL_VIRTUAL_WIDTH * 0.28f, ACTUAL_VIRTUAL_HEIGHT * 0.25f);
		batch.end();

		timer += delta;

		if (createPuzzle) {
			Locator.getGame().getGameScreen().startGame();
			createPuzzle = false;
		}
	}

	@Override
	public void resize(int width, int height) {
		stage.getViewport().update(width, height, true);
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNotify(Event event) {
		switch (event) {
		case EVENT_SHOW_LOAD_SCREEN:
			createPuzzle = true;
			if (Locator.adTimer >= Locator.MAX_AD_TIME) {
				Locator.getDevice().displayInterstitial();
				Locator.resetAdTimer();
			}
			break;
		case EVENT_SOLVER_DONE:
			gotoGameScreen();
			break;
		default:
			break;
		}

	}

	private void gotoGameScreen() {
		timer = 2;
		GameScreen next = Locator.getGame().getGameScreen();
		next.show();
		Locator.getGame().setScreen(next);
	}

	public void setBackground(BackgroundManager backgroundManager) {
		this.background = backgroundManager;

	}

}
